<?php

namespace app\index\controller;

class Classroom extends Base
{
    //前置方法
    protected $beforeActionList = [
        // 'right_hot',
        'right' => ['only' => 'index,detailed'],
        // 'learn' => ['only' => 'detailed'],
    ];


    /**
     * 学习记录
     */
    public function learn(){
        if(!empty(session('User', '', 'User')->id)){

           $name= db('researchlearn')
                ->where('tid',$this->request->param('id'))
                ->where('uid',session('User', '', 'User')->id)
                ->find();
            if (empty($name)) {
                $one = model('classroom')->field('id,title')->where('id', $this->request->param('id'))->find();

                $data = [
                    'title' => $one->title,
                    'tid' => $one->id,
                    'update_time' => time(),
                    'create_time' => time(),
                    'uid' => session('User', '', 'User')->id,
                    'url' => $this->request->url(),
                    'lang' => cookie('think_var'),
                ];
                db('researchlearn')->insert($data);
            }else{
                db('researchlearn')->where('id',$name['id'])->update(['create_time'=>time()]);

            }

        }
    }


    //右边最热，最新
    public function right_hot(){

        // 最新帖子
        $list = db('classroom')
            ->alias('a')
            ->join('__NAV__ c', 'c.id= a.cid AND c.lang= a.lang AND c.status = a.status')
            ->field('a.id,a.cid,a.title,c.model,a.create_time')
            ->where('a.status', '1')
            ->order('a.id DESC,a.update_time DESC')
            ->limit(10)
            ->select();
        //最新回复帖子
        $hot = db('classroom')
            ->alias('a')
            ->join('__NAV__ c', 'c.id= a.cid AND c.lang= a.lang AND c.status = a.status')
            ->field('a.id,a.cid,a.title,c.model,a.create_time')
            ->where('a.status', '1')
            ->order('a.update_time DESC,a.id DESC')
            ->limit(10)
            ->select();
        $this->assign([
            'hotten'=> $hot,
            'listten'=> $list,
        ]);
    }
}
